package com._54programer.ws.handler;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com._54programer.ws.Const;
import com._54programer.ws.Message;
import com._54programer.ws.NBind;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;

/**
 * 集群指令处理Handler
 * 
 * @author Administrator
 *
 */
@Slf4j
@Service(Const.CLUSTER_SERVICE)
public class ClusterMessageHandler {

	public void execute(JSONObject data) {
		
		Message messObj = JSON.toJavaObject(data, Message.class);
		log.info("消息来自ID：{}>>>>>>>>>>>",messObj.getServerId());
		switch (messObj.getMessageType()) {

		case Const.BY_GROUPID:// 群组groupId
			if (StringUtils.isBlank(messObj.getGroupId())) {
				break;
			}
			NBind.sendByGroup(messObj.getGroupId(), messObj.getData().toJSONString());
			break;
			
		case Const.BY_NONE:// 广播
			NBind.sendAll(messObj.getData().toJSONString());
			break;
			
		case Const.BY_UID:// Uid
			if (StringUtils.isBlank(messObj.getMessageTypeParams2())) {
				break;
			}
			NBind.sendByUid(messObj.getMessageTypeParams2(), messObj.getData().toJSONString());
			break;
			
		case Const.BY_USERNAME:// username
			if (StringUtils.isBlank(messObj.getMessageTypeParams2())) {
				break;
			}
			NBind.sendByUser(messObj.getMessageTypeParams2(), messObj.getData().toJSONString());
			break;

		default:
			break;
		}
	}
}
